package jco3.core.comkit;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Properties;

public class ConfigKit
{
  private static Properties properties = null;
  private static boolean isDev = true;

  static { if (properties == null) {
      loadPropertyFile("../appconfig/config.properties");
      isDev = !isPD();
    } }

  public static boolean isDev()
  {
    return isDev;
  }

  private static boolean isPD()
  {
    String pdips = getProperty("system.pdip");
    if (pdips.isEmpty()||pdips==null) return false; try
    {
      Enumeration en = 
        NetworkInterface.getNetworkInterfaces();
      Enumeration addresses;
      for (; en.hasMoreElements(); 
        addresses.hasMoreElements())
      {
        NetworkInterface networkinterface = (NetworkInterface)en.nextElement();
        addresses = networkinterface.getInetAddresses();
        continue;
      }
    }
    catch (SocketException e)
    {
      e.printStackTrace();
    }
    return false;
  }

  public static Properties loadPropertyFile(String file)
  {
    InputStream inputStream = null;
    try
    {
      inputStream = new FileInputStream(new File(file));
      Properties p = new Properties();
      p.load(inputStream);
      properties = p;
    } catch (FileNotFoundException e) {
      throw new IllegalArgumentException("Properties file not found: " + file);
    } catch (IOException e) {
      throw new IllegalArgumentException("Properties file can not be loading: " + file);
    } finally {
      try {
        if (inputStream != null) inputStream.close(); 
      }
      catch (IOException e) { e.printStackTrace(); }
    }
    if (properties == null)
      throw new RuntimeException("Properties file loading failed: " + file);
    return properties;
  }

  public static String getProperty(String key) {
    checkPropertyLoading();
    return properties.getProperty(key);
  }

  public static String getProperty(String key, String defaultValue) {
    checkPropertyLoading();
    return properties.getProperty(key, defaultValue);
  }

  public static Integer getPropertyToInt(String key) {
    checkPropertyLoading();
    Integer resultInt = null;
    String resultStr = properties.getProperty(key);
    if (resultStr != null)
      resultInt = Integer.valueOf(Integer.parseInt(resultStr));
    return resultInt;
  }

  public static Integer getPropertyToInt(String key, Integer defaultValue) {
    Integer result = getPropertyToInt(key);
    return result != null ? result : defaultValue;
  }

  public static Boolean getPropertyToBoolean(String key) {
    checkPropertyLoading();
    String resultStr = properties.getProperty(key);
    Boolean resultBool = null;
    if (resultStr != null) {
      if (resultStr.trim().equalsIgnoreCase("true"))
        resultBool = Boolean.valueOf(true);
      else if (resultStr.trim().equalsIgnoreCase("false"))
        resultBool = Boolean.valueOf(false);
    }
    return resultBool;
  }

  public static Boolean getPropertyToBoolean(String key, boolean defaultValue) {
    Boolean result = getPropertyToBoolean(key);
    return Boolean.valueOf(result != null ? result.booleanValue() : defaultValue);
  }
  private static void checkPropertyLoading() {
    if (properties == null)
      throw new RuntimeException("You must load properties file by invoking loadPropertyFile(String) method in configConstant(Constants) method before.");
  }
}